package com.vita.core1;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ExecutionException;

public class Schedul {
    /** 电梯列表 */
    private Elevator[] es;
    /** 总楼层 */
    private int totalFloors;
    /** 向上运行的任务 */
    private Queue<Integer> upList = new LinkedList<>();
    /** 向下运行的任务 */
    private Queue<Integer> dwList = new LinkedList<>();

    static final Object ob = "aa";

    public boolean remove(Queue<Integer> list, int i) throws InterruptedException {
        int time=0;
        while (true){
            boolean b = false;

            synchronized (ob){
                b = list.remove(i);
            }
            time = time + 100;
            if (b){
                return b;
            }
            if (time == 300){
                return b;
            }
            Thread.sleep(100);
        }
    }

    public Schedul(int totalFloors,Elevator... es ) {
        this.es = es;
        this.totalFloors = totalFloors;
    }

    public void run() throws ExecutionException, InterruptedException {
        for(Elevator w:this.es){
            w.init(this);
        }
        for (Elevator w:this.es){
            new Thread(()-> {
                try {
                    w.run().get();
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }

    public Elevator[] getEs() {
        return es;
    }

    public void setEs(Elevator[] es) {
        this.es = es;
    }

    public int getTotalFloors() {
        return totalFloors;
    }

    public void setTotalFloors(int totalFloors) {
        this.totalFloors = totalFloors;
    }

    public Queue<Integer> getUpList() {
        return upList;
    }

    public void setUpList(Integer... upList) {
        this.upList.addAll(Arrays.asList(upList));
    }

    public Queue<Integer> getDwList() {
        return dwList;
    }

    public void setDwList(Integer... dwList) {
        this.dwList.addAll(Arrays.asList(dwList));
    }
}
